import {
    AddContent, findContentById,
    removeContentByIds,
    showContentByCondition,
    showContentByPage,
    UpdateContent,
    updateContentState,
    uploadFile
} from "../../api/content";
import {message} from "antd";

export default {
    // 表格数据
    contentList:[],
    //选中行的key值
    selectRowKeys:[],
    //设置选中行的key值
    setSelectRowKeys(values){
        this.selectRowKeys=values
    },
    //获取所有内容
    getAllContent(){
        showContentByPage({page:1,limit:500}).then(res=>{
            console.log(res)
            if (res.code===200){
                this.contentList=res.data
            }else {
                return message.error('服务器异常！')
            }
        })
    },
    // 条件模糊查询内容
    queryContent(obj){
        showContentByCondition(obj).then(res=>{
            console.log(res)
            if (res.code===200){
                this.contentList=res.data
                return message.success('查询成功！')
            }else {
                return message.error('查询失败！')
            }
        })
    },
    // 批量修改内容状态
    setContentState(state){
        const obj={
            ids:this.selectRowKeys,
            state:state
        }
        updateContentState(obj).then(res=>{
            console.log(res)
            if (res.code===200){
                this.getAllContent()
                return message.success(state+'成功！')
            }else {
                return message.error(state+'失败！')
            }
        })
    },
    // 通过id删除/批量删除内容
    delContent(){
        removeContentByIds(this.selectRowKeys).then(res=>{
            console.log(res)
            if (res.code===200){
                this.getAllContent()
                return message.success('删除成功！')
            }else {
                return message.error('删除失败！')
            }
        })
    },
    //编辑内容
    editContent(obj,text){
        UpdateContent(obj).then(res=>{
            console.log(res)
            if (res.code===200){
                this.getAllContent()
                return message.success(text+'成功！')
            }else {
                return message.error(text+'失败！')
            }
        })
    },
    //上传文件
    uploadCover(file,obj,text='编辑'){

        if (file.name){
            uploadFile(file).then(res=>{
                console.log(res)
                obj.cover=res.data
                this.editContent(obj,text)
            }).catch(err=>{
                throw err
            })
        }else {
            this.editContent(obj,text)
        }

    },
    //添加内容
    addContent(obj,file){
        AddContent().then(res=>{
            console.log(res)
            this.getContentById(res.data,obj,file)
        })
    },
    //通过id查找内容
    getContentById(id,obj,file){
        findContentById(id).then(res=>{
            obj.id=id
            obj.album=res.data.album
            this.uploadCover(file,obj,'添加')
        })

    }
}